'use strict';
const Service = require('egg').Service;

class LoginController extends Service {
  // 客户端登录
  async login (data) {
    const ctx = this.ctx;
    const res = await this.app.mysql.select('users', { where: { username: data.username, password: data.password } });
    // const res = await this.app.mysql.query('select * from users where username=? and password=?', [username, password]);
    console.log('客户端用户信息:', res);
    // 如果查询匹配成功，服务端设置cookies
    if (res != null) {
      ctx.cookies.set('user', 'caijunyi');
      ctx.body = {
        status: 200,
        data: 'Cookie添加成功',
      };
    }
    return res;
  }

  // 后台管理员登录
  async admin_login (data) {
    console.log('后台用户信息:', data);
    const res = await this.app.mysql.select('admin_users', { where: { username: data.username, password: data.password } });

    // const username = data.username;
    // const password = data.password;
    // const res = await this.app.mysql.query('select * from admin_users where username=? and password=?', [username, password]);
    // if (res != null) {
    //   ctx.body = 'hi,egg';
    //   ctx.cookies.set('user', 'caijunyi');
    //   ctx.body = {
    //     status: 200,
    //     data: 'Cookie添加成功',
    //   };
    // }
    return res;
  }
  // 后台获取用户信息
  async admin_getuserinfo (data) {
    console.log('service-admin-login:', data);
    // const username = data.username;
    // const password = data.password;
    const res = await this.app.mysql.query('select * from admin_users where username=?', data.token);
    return res;
  }
}
module.exports = LoginController;
